import { createRouter,createWebHistory  } from "vue-router";
import useStore from "../store";


const routes = [
    {
        path: '/',
        redirect: '/todo/all'
      },
    {
    path:'/login',
    name:'Login',
    meta:{
        title:'登陆'
    },
    component:()  => import('../views/LoginView.vue'),
    beforeEnter:(to,form,next) =>{
        const token = useStore().user.token
        if(token)
        {
            next('/todo/all')
        }else{
            console.log(to,form)
            next()
        }
        }
    },
    {
    path:'/register',
    name:'register',
    meta:{
        title:'注册'
    },
    component:()  => import('../views/RegisterView.vue')
    },
    {
    path:'/',
    name:'Home',
    meta:{
        title:'平台导航页',
        transition: 'slide-right'
    },
    component:() => import('../views/Home.vue'),
    beforeEnter:(to,from,next) =>{
        const token = useStore().user.token
        
        if(token)
        {
            next()
        }else{
          
            next('/login')
        }
    },
    children:[
        {
            path:'/todo',
            name:'Todo',
            meta:{
                title:'待办管理'
            },
            component:()  => import('../views/index/Todo.vue'),
            children:[
                
               
                {
                    path:'/todo/all',
                    name:'TodoAll',
                    component:() => import('../views/index/todoviews/TodoAll.vue')
                },
            ]
        },
        {
            path:'/OA',
            name:'OA',
            component:() => import('../views/index/OA.vue'),
            children:[
                {
                    path:'/OA/notice',
                    name:'Notice',
                    component:() => import('../views/index/oaviews/Notice.vue')
                },{
                    path:'/OA/leave',
                    name:'Leave',
                    component:() => import('../views/index/oaviews/Leave.vue')
                }
            ]
        },
        {
            path:'/OA/department',
            name:'Department',
            component:() => import('../views/index/Department.vue'),
            children:[
                {
                    path:'/department/formapply',
                    name:'FormApply',
                    component:() => import('../views/index/departmentviews/FormApply.vue')
                },{
                    path:'/department/stafflist',
                    name:'StaffList',
                    component:() => import('../views/index/departmentviews/StaffList.vue')
                },{
                    path:'/department/departmentlist',
                    name:'DepartmentList',
                    component:() => import('../views/index/departmentviews/DepartmentList.vue')
                }
            ]
        },
        {
            path:'/dashboard',
            name:'Dashboard',
            meta:{
                title:'数据统计页'
            },
            component:()  => import('../views/index/TodoDashboard.vue')
        },{
            path:'/setting',
            name:'Settings',
            meta:{
                title:'设置'
            },
            component:()  => import('../views/index/Settings.vue')
        },{
            path:'/about',
            name:'About',
            meta:{
                title:'关于'
            },
            component:()  => import('../views/index/About.vue')
        },{
            meta:{
                title:'历史'
            },
            path:'/history',
            name:'History',
            component:()  => import('../views/index/History.vue'),
            children:[
                
                {
                    path:'/history/doing',
                    name:'Doing',
                    component:() => import('../views/index/historyviews/Doing.vue')
                },
                {
                    path:'/history/expired',
                    name:'Expired',
                    component:() => import('../views/index/historyviews/Expired.vue')
                },{
                    path:'/history/finished',
                    name:'Finished',
                    component:() => import('../views/index/historyviews/Finished.vue')
                },{
                    path:'/history/myjoin',
                    name:'MyJoin',
                    component:() => import('../views/index/historyviews/MyJoin.vue')
                }
            ]
        
        },{
            path:'/sendmsg',
            name:'SendMsg',
            meta:{
                title:'发送'
            },
            component:()  => import('../views/index/SendMsg.vue')
        },
        {
            path:'/userlist',
            name:'UserList',
            meta:{
                title:'用户列表'
            },
            component:()  => import('../views/index/UserList.vue')
        },
        {
            meta:{
                title:'个人资料'
            },
            path:'/userinfo',
            name:'UserInfo',
            component:()  => import('../views/index/UserInfo.vue')
        },
        {
            path:'/blank',
            name:"blak",
            component:() => import ('../views/index/blank.vue')
        }
    ]
},
]


const router = createRouter({
    history: createWebHistory(),
    routes,
});



router.onError((error) => {
    const pattern = /Loading chunk (\d)+ failed/g;
    const isChunkLoadFailed = error.message.match(pattern);
    const targetPath = router.history.pending.fullPath;
    if (isChunkLoadFailed) {
      router.replace(targetPath);
    }
  })



  

export default router;